package com.etrans.web.gps.business.video.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etrans.egov.base.intf.ObjDriver;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.gis.domain.TrackPlayBack;
import com.etrans.web.gps.business.gis.service.TrackPlayBackService;
import com.etrans.web.gps.business.report.domain.HistoryPicVO;
import com.etrans.web.gps.business.report.service.HisPicService;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.business.video.domain.TrackVO;
import com.etrans.web.gps.business.video.domain.VehicleVO;
import com.etrans.web.gps.business.video.domain.VideoAlarmVO;
import com.etrans.web.gps.business.video.domain.VideoVO;
import com.etrans.web.gps.business.video.service.VideoService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.grid.FlexiGridUtil;


@Controller
@RequestMapping("/video")
public class VideoController extends BaseController{

    @Autowired
    private VideoService videoService;

    @Autowired
    private TrackPlayBackService trackPlayBackService;
    
    @Autowired
	private HisPicService hisPicService;
    
    @Autowired
	private Config config; //引用统一的参数配置类 
    
    /**
	 * 视频回放
	 * @Description 页面跳转
	 * @param vehicleID
	 * @return 
	 */
	@RequestMapping("hisVideo.html")
	public String hisVideo(Model model,String vehicleID) {
		HashMap<String,Object> params =  new HashMap<String,Object>(); 
		String returnUrl="views/business/video/hisVideoSD";
		params.put("vehicleID", vehicleID);
		VideoVO video=videoService.queryVehicleDeviceSetup(params);
 
        if(null!=video){
        	String paramValue = video.getParamvalue();
        	if(paramValue!=null){
        		String[] pv = paramValue.split(",");
    			for (int i = 0; i <= pv.length - 1; i++) {
    				String[] pvDetail = pv[i].split("=");
    				if (pvDetail.length > 1) {
    					pvDetail[0] = pvDetail[0].equals("IP:TransmitIP") ? "TransmitIP"
    							: pvDetail[0];
    					model.addAttribute(pvDetail[0], pvDetail[1]);
    				}
    			}
        	}
        	model.addAttribute("vehicleID", vehicleID);
        	model.addAttribute("commno", video.getCommno());
        	model.addAttribute("registrationNO", video.getRegistrationNO());
        	model.addAttribute("internal_number", video.getInternal_number());
        	model.addAttribute("ocxPath", config.ocxPath);
        	if(video.getIsPublicNet()==1||video.getIsPublicNet()==2){
        		returnUrl="views/business/video/hisVideoSD";
        	}
        	if(video.getIsPublicNet()==4){
        		returnUrl="views/business/video/hisVideoHK";
        	}
        	if(video.getIsPublicNet()==3){
        		returnUrl="views/business/video/hisVideoHD";
        	}
        	/*if(video.getIsPublicNet()==5){
        		returnUrl="views/business/video/hisVideoRM";
        	}*/
        }
        
        return returnUrl;  
		
	}
	
	
	/**
	 * 视频监控
	 * @Description 页面跳转
	 * @param vehicleID
	 * @return 
	 */
	@RequestMapping("videoMonitorOps.jhtml")
	public String   videoMonitorOps(Model model,String vehicleID) {
		HashMap<String,Object> params =  new HashMap<String,Object>(); 
		String returnUrl="views/business/video/videoSDops";
		params.put("vehicleID", vehicleID);
		VideoVO video=videoService.queryVehicleDeviceSetup(params);
        if(null!=video){
        	String paramValue = video.getParamvalue();
        	if(paramValue!=null){
        		String[] pv = paramValue.split(",");
    			for (int i = 0; i <= pv.length - 1; i++) {
    				String[] pvDetail = pv[i].split("=");
    				if (pvDetail.length > 1) {
    					pvDetail[0] = pvDetail[0].equals("IP:TransmitIP") ? "TransmitIP"
    							: pvDetail[0];
    					model.addAttribute(pvDetail[0], pvDetail[1]);
    				}
    			}
        	}
        	model.addAttribute("ocxPath", config.ocxPath);
        	model.addAttribute("vehicleID", vehicleID);
        	model.addAttribute("commno", video.getCommno());
        	model.addAttribute("registrationNO", video.getRegistrationNO());
        	model.addAttribute("internal_number", video.getInternal_number());
        	if(video.getIsPublicNet()==1||video.getIsPublicNet()==2){
        		returnUrl="views/business/video/videoSDops";
        	}else if(video.getIsPublicNet()==4){
        		returnUrl="views/business/video/videoHKops";
        	}else if(video.getIsPublicNet()==3){
        		returnUrl="views/business/video/videoHDops";
        	}else if(video.getIsPublicNet()==5){
        		returnUrl="views/business/video/videoRMops";
        	}else{
        		returnUrl="views/business/video/videoSDops";
        	}
        }
        
        return returnUrl;   
		
	}
	
	
	/**
	 * 视频监控
	 * @Description 页面跳转
	 * @param vehicleID
	 * @return 
	 */
	@RequestMapping("videoMonitor.html")
	public String   videoMonitor(Model model,String vehicleID) {
		HashMap<String,Object> params =  new HashMap<String,Object>(); 
		String returnUrl="views/business/video/videoSD";
		params.put("vehicleID", vehicleID);
		VideoVO video=videoService.queryVehicleDeviceSetup(params);
        if(null!=video){
        	String paramValue = video.getParamvalue();
        	if(paramValue!=null){
        		String[] pv = paramValue.split(",");
    			for (int i = 0; i <= pv.length - 1; i++) {
    				String[] pvDetail = pv[i].split("=");
    				if (pvDetail.length > 1) {
    					pvDetail[0] = pvDetail[0].equals("IP:TransmitIP") ? "TransmitIP"
    							: pvDetail[0];
    					model.addAttribute(pvDetail[0], pvDetail[1]);
    				}
    			}
        	}
        	model.addAttribute("ocxPath", config.ocxPath);
        	model.addAttribute("vehicleID", vehicleID);
        	model.addAttribute("commno", video.getCommno());
        	model.addAttribute("registrationNO", video.getRegistrationNO());
        	model.addAttribute("internal_number", video.getInternal_number());
        	if(video.getIsPublicNet()==1||video.getIsPublicNet()==2){
        		returnUrl="views/business/video/videoSD";
        	}else if(video.getIsPublicNet()==4){
        		returnUrl="views/business/video/videoHK";
        	}else if(video.getIsPublicNet()==3){
        		returnUrl="views/business/video/videoHD";
        	}else if(video.getIsPublicNet()==5){
        		returnUrl="views/business/video/videoRM";
        	}else{
        		returnUrl="views/business/video/videoSD";
        	}
        }
        
        return returnUrl;   
		
	}
	
	/**
	 * 多车视频监控
	 * @Description iFrame页面跳转
	 * @param vehicleID 车辆ID，position 窗口位置
	 * @return 
	 */
	@RequestMapping("videoMonitorMin.html")
	public String videoMonitorMin(Model model,String vehicleID,String position) {
		HashMap<String,Object> params =  new HashMap<String,Object>(); 
		String returnUrl="views/business/video/videomin";
		model.addAttribute("position", position);
		if(null==vehicleID||"".equals(vehicleID)){
			
		}else{
			params.put("vehicleID", vehicleID);
			VideoVO video=videoService.queryVehicleDeviceSetup(params);
	        if(null!=video){
	        	String paramValue = video.getParamvalue();
	        	if(paramValue!=null){
	        		String[] pv = paramValue.split(",");
	    			for (int i = 0; i <= pv.length - 1; i++) {
	    				String[] pvDetail = pv[i].split("=");
	    				if (pvDetail.length > 1) {
	    					pvDetail[0] = pvDetail[0].equals("IP:TransmitIP") ? "TransmitIP"
	    							: pvDetail[0];
	    					model.addAttribute(pvDetail[0], pvDetail[1]);
	    				}
	    			}
	        	}
	        	model.addAttribute("ocxPath", config.ocxPath);
	        	model.addAttribute("vehicleID", vehicleID);
	        	model.addAttribute("commno", video.getCommno());
	        	model.addAttribute("registrationNO", video.getRegistrationNO());
	        	model.addAttribute("internal_number", video.getInternal_number());
	        	model.addAttribute("vehicle_color",video.getVehicleColor());
	        	model.addAttribute("TEXT",video.getTEXT());
	        	if(video.getIsPublicNet()==1||video.getIsPublicNet()==2){
	        		returnUrl="views/business/video/videoSDmin";//顺达
	        	}else if(video.getIsPublicNet()==4){
	        		returnUrl="views/business/video/videoHKmin";//海康
	        	}else if(video.getIsPublicNet()==3){
	        		if(position.equals("1")){
	        			returnUrl="views/business/video/videoHDmin1";
	        		}else if(position.equals("2")){
	        			returnUrl="views/business/video/videoHDmin2";
	        		}else if(position.equals("3")){
	        			returnUrl="views/business/video/videoHDmin3";
	        		}else{
	        			returnUrl="views/business/video/videoHDmin";
	        		}
	        	/*}else if(video.getIsPublicNet()==5){
	        		returnUrl="views/business/video/videoRMmin";*/
	        	}else{
	        		returnUrl="views/business/video/videoSDmin";
	        	}
	        }
		}
	      
		
        return returnUrl; 
	}
	
	/**
	 * 视频车辆列表
	 * @Description 加载车辆列表
	 * @param 
	 * @return 车辆list
	 */
	@RequestMapping("queryVideoVehicleList.html")
	@ResponseBody
	public List<VehicleVO> queryVideoVehicleList() {
			HashMap<String,Object> params= new HashMap<String,Object>();
			SysUser user = (SysUser)getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
			params.put("fullID", user.getFullID());
			List<VehicleVO> list=videoService.queryVideoVehicleList(params);
			VehicleVO vehicle= new VehicleVO();
			vehicle.setText("请选择车辆");
			vehicle.setId(0);
			list.add(0, vehicle);
		return list;
	}
	
	/**
	 * 判断车辆是否有视频
	 * @Description 判断车辆是否有视频
	 * @param 
	 * @return 车辆list
	 */
	@RequestMapping("queryVideoVehicleIsOn.html")
	@ResponseBody
	public List<VehicleVO> queryVideoVehicleIsOn(String vehicleID) {
			HashMap<String,Object> params= new HashMap<String,Object>();
			params.put("vehicleID", vehicleID);
			SysUser user = (SysUser)getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
			params.put("userId",user.getId());
			List<VehicleVO> list=videoService.queryVideoVehicleIsOn(params);
		return list;
	}
	
	
	/**
	 * 视频车辆轨迹列表
	 * @Description 加载车辆轨迹列表
	 * @param 
	 * @return 车辆轨迹list
	 */
	@RequestMapping("queryVideoVehicleTrackList.html")
	@ResponseBody
	public List<TrackVO> queryVideoVehicleTrackList(String vehicleID) {
			HashMap<String,Object> params= new HashMap<String,Object>();
			List<TrackVO> list=videoService.queryVideoVehicleTrackList(params);
	
		return list;
	}
	
	/**
	 * @Description 视频车辆轨迹列表(直接渲染flexigrid)
	 *  @param paramsGrid 
	 */
	@RequestMapping("queryVideoVehicleTrackListForGrid.html")
	@ResponseBody
	public Map<String, Object> queryVideoVehicleTrackListForGrid(String paramsGrid){
		Map<String, Object> reslut = new HashMap<>();
		Map<String, Object> param =  FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
//		param.put("vehicleID", "29012");
//		param.put("startTime", "2017-07-01 00:00:00");
//		param.put("endTime", "2017-08-01 00:00:00");
		List<VideoAlarmVO> videoTiredAlarmList=videoService.queryVideoTiredAlarmList(param);
		List<VideoAlarmVO> videoNightAlarmList=videoService.queryVideoNightAlarmList(param);
		List<VideoAlarmVO> videoSpeedAlarmList=videoService.queryVideoSpeedAlarmList(param);
		List<VideoAlarmVO> videoRoadLevelSpeedAlarmList=videoService.queryVideoRoadLevelSpeedAlarmList(param);
		
		List<HistoryPicVO> picList = hisPicService.queryPicListByVehicleId(param.get("vehicleID").toString(), param.get("startTime").toString(), param.get("endTime").toString());
		List<TrackVO> list=new ArrayList<TrackVO>();
    	List<TrackPlayBack> lisTrackPlayBacks = trackPlayBackService.selectPlayBackTrack(param.get("vehicleID").toString(), param.get("startTime").toString(), param.get("endTime").toString(), -1, -1);
    	int n=1;
    	for(TrackPlayBack track : lisTrackPlayBacks){
    		TrackVO temp = new TrackVO();
    		if(n==1){
    			temp.setTiredAlarmList(videoTiredAlarmList);
    			temp.setSpeedAlarmList(videoSpeedAlarmList);
    			temp.setNightAlarmList(videoNightAlarmList);
    			temp.setRoadLevelSpeedAlarmList(videoRoadLevelSpeedAlarmList);
    			temp.setHisPicList(picList);
    		}
    		temp.setId(n);
    		temp.setRegistrationNO(param.get("registrationNO").toString());
    		temp.setInternal_number(param.get("internal_number").toString());
    		temp.setGps_time(track.getGps_time());
    		temp.setGps_speed(track.getGps_speed());
    		temp.setGps_mileage(track.getGps_mileage());
    		temp.setLongitude(track.getLongitude());
    		temp.setLatitude(track.getLatitude());
    		temp.setVehicle_status(track.getStatus());
    		list.add(temp);
    		n++;
    		if(n>100){
    			break;
    		}
    	}
		reslut.put("rows", list);
        return  reslut;
	}
	
	
	/**
	 * 多车视频监控
	 * @Description 条状
	 * @param vehicleID，可以用逗号隔开，支持1-4个车辆ID，多传不解析
	 * @return 多车监控页面
	 */
	@RequestMapping("videoMonitorMT.html")
	public String videoMonitorMT(Model model,String vehicleID) {
		String returnUrl="views/business/video/videoMulti";
		model.addAttribute("ocxPath", config.ocxPath);
		if(null==vehicleID||"".equals(vehicleID)){
			
		}else{
			String []str=vehicleID.split(",");
			if(str.length>0){
				model.addAttribute("vehicleID1", str[0]);
			}else{
				model.addAttribute("vehicleID1", "");
			}
			if(str.length>1){
				model.addAttribute("vehicleID2", str[1]);
			}else{
				model.addAttribute("vehicleID2", "");
			}
			if(str.length>2){
				model.addAttribute("vehicleID3", str[2]);
			}else{
				model.addAttribute("vehicleID3", "");
			}
			if(str.length>3){
				model.addAttribute("vehicleID4", str[3]);
			}else{
				model.addAttribute("vehicleID4", "");
			}
		}
        return returnUrl; 
	}
	
	@RequestMapping("queryDriverInfomation.html")
	@ResponseBody
	public ObjDriver queryDriverInfomation(String[] vehicle_no_color) throws Exception{
		return videoService.getDrivserInfo(vehicle_no_color,request);
	}
	/**
	 * 多车视频监控FLASH跳转
	 * @Description 
	 * @param 
	 * @return
	 */
	@RequestMapping("videoMonitorMultiFlash.html")
	public String videoMonitorFlash(Model model,String vehicleID) {
		String returnUrl="views/business/video/videoMultiFlash";
		if(null==vehicleID||"".equals(vehicleID)){
			
		}else{
			String []str=vehicleID.split(",");
			if(str.length>0){
				model.addAttribute("vehicleID1", str[0]);
			}else{
				model.addAttribute("vehicleID1", "");
			}
			if(str.length>1){
				model.addAttribute("vehicleID2", str[1]);
			}else{
				model.addAttribute("vehicleID2", "");
			}
			if(str.length>2){
				model.addAttribute("vehicleID3", str[2]);
			}else{
				model.addAttribute("vehicleID3", "");
			}
			if(str.length>3){
				model.addAttribute("vehicleID4", str[3]);
			}else{
				model.addAttribute("vehicleID4", "");
			}
		}
        return returnUrl; 
	}
	/**
	 * 单车监控FLASH
	 * @Description iFrame页面跳转
	 * @param vehicleID 车辆ID，position 窗口位置
	 * @return 
	 */
	@RequestMapping("videoMonitorMinFlash.html")
	public String videoMonitorMinFlash(Model model,String vehicleID,String position) {
		String returnUrl="views/business/video/videominFlash";
		model.addAttribute("position", position);
		if(null==vehicleID||"".equals(vehicleID)){
			
		}else{
			String []str=vehicleID.split(",");
			if(str.length>0){
				model.addAttribute("vehicleID1", str[0]);
			}else{
				model.addAttribute("vehicleID1", "");
			}
			if(str.length>1){
				model.addAttribute("vehicleID2", str[1]);
			}else{
				model.addAttribute("vehicleID2", "");
			}
			if(str.length>2){
				model.addAttribute("vehicleID3", str[2]);
			}else{
				model.addAttribute("vehicleID3", "");
			}
			if(str.length>3){
				model.addAttribute("vehicleID4", str[3]);
			}else{
				model.addAttribute("vehicleID4", "");
			}
		}
        return returnUrl; 
	}
	/**
	 * 单车车视频监控FLASH
	 * @Description iFrame页面跳转
	 * @param vehicleID 车辆ID，position 窗口位置
	 * @return 
	 */
	@RequestMapping("videoMonitorFlash.html")
	public String videoMonitorFlash(Model model,String vehicleID,String position) {
		HashMap<String,Object> params =  new HashMap<String,Object>();
		String returnUrl="views/business/video/videoFlash";
		model.addAttribute("position", position);
		params.put("vehicleID", vehicleID);
		VideoVO video=videoService.queryVehicleVideoInfo(params);
		if(null==vehicleID||"".equals(vehicleID)){
			
		}else{
			String []str=vehicleID.split(",");
			if(str.length>0){
				model.addAttribute("vehicleID1", str[0]);
			}else{
				model.addAttribute("vehicleID1", "");
			}
			if(str.length>1){
				model.addAttribute("vehicleID2", str[1]);
			}else{
				model.addAttribute("vehicleID2", "");
			}
			if(str.length>2){
				model.addAttribute("vehicleID3", str[2]);
			}else{
				model.addAttribute("vehicleID3", "");
			}
			if(str.length>3){
				model.addAttribute("vehicleID4", str[3]);
			}else{
				model.addAttribute("vehicleID4", "");
			}
		}
		model.addAttribute("vehicleID", vehicleID);
    	model.addAttribute("vehicle_color",video.getVehicleColor());
    	model.addAttribute("TEXT",video.getTEXT());
    	model.addAttribute("platurl",video.getPlat_url());
        return returnUrl; 
	}
	
}
